﻿package  
{
	import com.greensock.*;
	import com.greensock.easing.*;
	import com.utils.DebugTrace;
	import com.utils.GetMovieClip;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import picPoll.Poll;
	/**
	 * ...
	 * @author zhengzihua
	 */
	public class ShowLightBall extends Sprite
	{
		private var _currentBall:int = 0;
		private var mc:MovieClip;
		private var tranLeftBtn:MovieClip;
		private var tranRightBtn:MovieClip;
		
		private var fun:Function;
		private var timer:Timer;
		private var ballNum:int;
		private var poll:Poll;
		public function ShowLightBall(sprite:Poll) 
		{
			poll=sprite;
			mc=GetMovieClip.getMovieClip("LightBall");
			tranLeftBtn=GetMovieClip.getMovieClip("TransparentBtn");
			tranRightBtn=GetMovieClip.getMovieClip("TransparentBtn");
			
			addChild(mc);
			addChild(tranLeftBtn);
			addChild(tranRightBtn);
			tranLeftBtn.x=-50;
			tranRightBtn.x=400;
		}
		public function addEvent():void{
			tranLeftBtn.addEventListener(MouseEvent.MOUSE_OUT,onRollOut);
			tranRightBtn.addEventListener(MouseEvent.MOUSE_OUT,onRollOut);
			tranLeftBtn.addEventListener(MouseEvent.MOUSE_OVER,onRollOver);
			tranRightBtn.addEventListener(MouseEvent.MOUSE_OVER,onRollOver);
		}
		private function delEvent():void{
			tranLeftBtn.removeEventListener(MouseEvent.MOUSE_OUT,onRollOut);
			tranRightBtn.removeEventListener(MouseEvent.MOUSE_OUT,onRollOut);
			tranLeftBtn.removeEventListener(MouseEvent.MOUSE_OVER,onRollOver);
			tranRightBtn.removeEventListener(MouseEvent.MOUSE_OVER,onRollOver);
		}
		private function onRollOver(e:MouseEvent=null):void{
			if(e.currentTarget==tranLeftBtn){
				fun=leftBall;
			}else if(e.currentTarget==tranRightBtn){
				fun=rightBall;
			}
			moveLightBall(fun);
		}
		private function onRollOut(e:MouseEvent=null):void{
			if(timer==null)return;
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER,fun);
			timer=null;
		}
		private function moveLightBall(fun:Function):void{
			timer=new Timer(100,0);
			timer.start();
			timer.addEventListener(TimerEvent.TIMER,fun);
		}
		private function leftBall(e:TimerEvent=null):void{
			dispatchEvent(new Event(StaticInfo.LEFT_BALL));
		}
		private function rightBall(e:TimerEvent=null):void{
			dispatchEvent(new Event(StaticInfo.RIGHT_BALL));
		}
		public function delThis():void{
			delEvent();
			onRollOut();
			delBallClick();
			if(parent){
				parent.removeChild(this);
			}
		}
		public function setBallSize(num:int,b:Boolean):void{
			if(num<6)num=6;
			ballNum=num;
			for(var i:int;i<num;i++){
				var ball:MovieClip=mc.balls["b"+i] as MovieClip;
				var size:Number=int(Math.random()*50+20)/100;
				ball.gotoAndStop(1);
				ball.num=i;
				if(b){
					ball.scaleX=size;
					ball.scaleY=size;
					ball.addEventListener(MouseEvent.CLICK,onBallClick);
					ball.visible=true;
					ball.buttonMode=true;
				}else{
					ball.scaleX=1;
					ball.scaleY=1;
				}
			}
			for(var j:int=num;j<16;j++){
				var ball2:MovieClip=mc.balls["b"+j] as MovieClip;
				if(ball2!=null)ball2.visible=false;
			}
		} 
		private function onBallClick(e:MouseEvent=null):void{
			var ball:MovieClip=e.currentTarget as MovieClip;
			poll.currentNum=ball.num;
		}
		private function delBallClick():void{
			if(ballNum==0)return;
			for(var i:int=0;i<ballNum;i++){
				var ball:MovieClip=mc.balls["b"+i] as MovieClip;
				ball.removeEventListener(MouseEvent.CLICK,onBallClick);
			}
			ballNum=0;
		}
		public function get currentBall():int { return _currentBall; }
		
		public function set currentBall(value:int):void 
		{
			var ball1:MovieClip=mc.balls["b"+_currentBall];
			ball1.gotoAndPlay("black");
			
			_currentBall = value;
			var ball2:MovieClip=mc.balls["b" + value];
			ball2.gotoAndPlay("red");
			if(value==0){
				moveBalls(7);
				return;
			}
			var cx:int=mc.balls.x+ball2.x;
			if(cx>390){
				var num:int=mc.balls.x-ball2.x+ball1.x;
				moveBalls(num);
			}else if(cx<0){
				var ball0:MovieClip=mc.balls["b"+(value-1)];
				var num2:int;
				if(ball0==null){
					num2=7;
				}else{
					num2=mc.balls.x+(ball2.x-ball0.x+7);
				}
				moveBalls(num2);
			}
			
		}
		private function moveBalls(num:int):void{
			DebugTrace.dtrace("code info ShowLightBall.as:移动到x--"+num);
			TweenMax.to(mc.balls, 0.5, {bezier:[{x:num}], orientToBezier:true, ease:Cubic.easeOut});
		}
		
	}

}